package svc

import (
	"context"
	"gozero-microservices/inventory-service/rpc/internal/config"
	"gozero-microservices/inventory-service/rpc/internal/model"

	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

type ServiceContext struct {
	Config config.Config
	Logger logx.Logger

	// 注入模型
	GoodsSkuModel model.MallGoodsSkuModel
	BarrierModel  model.MallBarrierModel
}

func NewServiceContext(c config.Config) *ServiceContext {
	// ------------- 获取mysql连接开始 -------------
	datasource := ""
	switch c.Env {
	case "dev":
		datasource = c.MysqlConf.Dev.DataSource
	case "test":
		datasource = c.MysqlConf.Test.DataSource
	case "prod":
		datasource = c.MysqlConf.Prod.DataSource
	}
	sc := sqlx.NewMysql(datasource)
	// ------------- 获取mysql连接结束 -------------

	return &ServiceContext{
		Config: c,
		Logger: logx.WithContext(context.Background()),

		// 创建模型
		GoodsSkuModel: model.NewMallGoodsSkuModel(sc),
		BarrierModel:  model.NewMallBarrierModel(sc),
	}
}
